package com.wsjzzcbq.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.wsjzzcbq.service.SentinelService;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * SentinelServiceImpl
 *
 * @author wsjz
 * @date 2023/10/08
 */
@Service
public class SentinelServiceImpl implements SentinelService {

    //@SentinelResource(value = "user-name-resource", blockHandler = "exceptionHandler", fallback = "userNameFallback")
    @Override
    public String userName() {
        int a = 1/0;
        return "小娃撑小艇，偷采白莲回。不解藏踪迹，浮萍一道开。";
    }

    // Block 异常处理函数，参数最后多一个 BlockException，其余与原函数一致.
    public String exceptionHandler(String name, BlockException ex) {
        // Do some log here.
        ex.printStackTrace();
        return "Oops, error occurred at " + name;
    }

    // Fallback 函数，函数签名与原函数一致或加一个 Throwable 类型的参数.
    public String userNameFallback(String name) {
        return String.format("Halooooo %d", name);
    }

    public String userNameBlockHandler(String name, Throwable t) {
        System.out.println(name);
        return "默认返回值";
    }


    @Override
    public String userAge() {
        return "我的年龄是18岁";
    }

    @Override
    public String userAddr() {
        Random random = new Random();
        int value = random.nextInt(100);
        if (value > 50) {
            System.out.println(value);
            int a = 1/0;
        }
        return "我在遥远的地方";
    }
}
